local LaserDots = {}

function onPlayerChangeWeapon()
	setPedWeaponSlot(getLocalPlayer(), 2)
end
addEventHandler("onClientPlayerWeaponSwitch", getRootElement(), onPlayerChangeWeapon)

function onPlayerAimWeapon(_, state)
	if state == "down" then setElementData(getLocalPlayer(), "laseraim", true, true) end
	if state == "up" then setElementData(getLocalPlayer(), "laseraim", false, true) end
end
bindKey("aim_weapon", "both", onPlayerAimWeapon)

function onPlayerGG()
	if getControlState("aim_weapon") then
		local getObject = getPedTarget(getLocalPlayer())
		
		if getObject and getElementType(getObject) == "object" then
			local x1,y1,z1 = getElementPosition(getObject)
		end
	
	end
	
	-- Laser
	for _, player in ipairs(getElementsByType("player")) do
		if getElementData(player, "laseraim") then
			local x1, y1, z1 = getPedWeaponMuzzlePosition(player)
			local x2, y2, z2 = getPedTargetEnd(player)
			local x3, y3, z3 = getPedTargetCollision(player)
			if x3 and y3 and z3 then
				dxDrawLine3D(x1, y1, z1, x3, y3, z3, tocolor(0, 0, 255, 255), 3)
				dxDrawLine3D(x1, y1, z1, x3, y3, z3, tocolor(255, 255, 255, 255), 1)
				if not LaserDots[player] then
					LaserDots[player] = createMarker(x3, y3, z3, "corona", 0.05, 0, 255, 255, 255)
				else
					setElementPosition(LaserDots[player], x3, y3, z3)
				end
			elseif x2 and y2 and z2 then
				dxDrawLine3D(x1, y1, z1, x2, y2, z2, tocolor(0, 0, 255, 255), 3)
				dxDrawLine3D(x1, y1, z1, x2, y2, z2, tocolor(255, 255, 255, 255), 1)
				if LaserDots[player] and isElement(LaserDots[player]) then
					destroyElement(LaserDots[player])
					LaserDots[player] = nil
				end
			end
		else
			if LaserDots[player] and isElement(LaserDots[player]) then
				destroyElement(LaserDots[player])
				LaserDots[player] = nil
			end
		end
	end
end
addEventHandler("onClientRender", getRootElement(), onPlayerGG)

function onPlayerClick()
	if not getControlState("aim_weapon") then return end
	
	
end
bindKey("fire", "down", onPlayerClick)

function onPressUndo()
	if getKeyState("lctrl") or getKeyState("rctrl") then
		
	end
end
bindKey("z", "down", onPressUndo)

function onPressRedo()
	if getKeyState("lctrl") or getKeyState("rctrl") then
		
	end
end
bindKey("x", "down", onPressRedo)

function onDownloaded()
	triggerServerEvent("onPlayerDownloaded", getRootElement(), getLocalPlayer())
	
	
	for i=300, 25000 do removeWorldModel(i, 10000, 0, 0, 0) end
	setWaterLevel(-1000000)
	setElementFrozen(localPlayer, true)
	setElementPosition(localPlayer, 0, 0, 5)
end
addEventHandler("onClientResourceStart", getResourceRootElement(getThisResource()), onDownloaded)


--for i=300, 25000 do removeWorldModel(i, 10000, 0, 0, 0) end

-- Testing for google...